package com.ayu.shortlink.settlement.mq.consumer;

import com.ayu.shortlink.settlement.common.convention.exception.ServiceException;
import com.ayu.shortlink.settlement.common.enums.TradeStatusEnum;
import com.ayu.shortlink.settlement.common.enums.UserCouponStatusEnum;
import com.ayu.shortlink.settlement.dao.entity.OrderDO;
import com.ayu.shortlink.settlement.dao.entity.UserCouponDO;
import com.ayu.shortlink.settlement.dao.mapper.OrderMapper;
import com.ayu.shortlink.settlement.dao.mapper.UserCouponMapper;
import com.ayu.shortlink.settlement.mq.basis.base.MessageWrapper;
import com.ayu.shortlink.settlement.mq.basis.event.CloseOrderEvent;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import static com.ayu.shortlink.settlement.common.constant.RocketMQConstant.GROUP_CLOSE_ORDER;
import static com.ayu.shortlink.settlement.common.constant.RocketMQConstant.TOPIC_CLOSE_ORDER;

/**
 * 关闭订单消费者
 */
@Component
@RequiredArgsConstructor
@Slf4j
@RocketMQMessageListener(
        topic = TOPIC_CLOSE_ORDER,
        consumerGroup = GROUP_CLOSE_ORDER
)
public class CloseOrderConsumer implements RocketMQListener<MessageWrapper<CloseOrderEvent>> {
    private final OrderMapper orderMapper;
    private final UserCouponMapper userCouponMapper;
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onMessage(MessageWrapper<CloseOrderEvent> closeOrderEventMessageWrapper) {
        CloseOrderEvent message = closeOrderEventMessageWrapper.getMessage();

        LambdaQueryWrapper<OrderDO> queryWrapper = Wrappers.lambdaQuery(OrderDO.class)
                .eq(OrderDO::getUserId, message.getUserId())
                .eq(OrderDO::getOrderSn, message.getOrderSn());

        OrderDO orderDO = orderMapper.selectOne(queryWrapper);
        if (orderDO == null){
            log.error("订单查询失败,orderSn:{}",message.getOrderSn());
            throw new ServiceException("订单查询失败");
        }
        // 如果订单还是等待支付 那么就进行订单的关闭
        if (orderDO.getStatus().equals(TradeStatusEnum.WAIT_BUYER_PAY.tradeCode())){
            LambdaUpdateWrapper<OrderDO> updateWrapper = Wrappers.lambdaUpdate(OrderDO.class)
                    .eq(OrderDO::getUserId, message.getUserId())
                    .eq(OrderDO::getOrderSn, message.getOrderSn())
                    .set(OrderDO::getStatus, TradeStatusEnum.TRADE_CLOSED.tradeCode());
            orderMapper.update(updateWrapper);

            // 将优惠券进行解锁
            if (orderDO.getUserCouponId() != null){
                LambdaUpdateWrapper<UserCouponDO> couponDOLambdaUpdateWrapper = Wrappers.lambdaUpdate(UserCouponDO.class)
                        .eq(UserCouponDO::getUserId, message.getUserId())
                        .eq(UserCouponDO::getId, orderDO.getUserCouponId())
                        .set(UserCouponDO::getStatus, UserCouponStatusEnum.UNUSED);
                userCouponMapper.update(couponDOLambdaUpdateWrapper);
            }
        }
    }
}
